package LeetCode.leetcode;

/**
 * 颠倒二进制位
 * @author: leiwenkang02@meituan.com
 * @date: 2019/1/8
 * @time: 20:13
 */
public class Test190颠倒二进制位 {
    public static void main(String[] args) {
        //11111111111111111111111111111101
        System.out.println(reverseBits(-3));
//        System.out.println(-4 >>> 1);
        //10111111111111111111111111111111
    }
    public static int reverseBits(int n) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            result += n & 1;
            n >>= 1;   // CATCH: must do unsigned shift
            if (i < 31) // CATCH: for last digit, don't shift!
                result <<= 1;
        }
        return result;
    }
}
